package components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.swing.Timer;


import components.BasalProfile;

public class InsulinManager {
	private final int dailyMax = 200; // takes into account previous basal and bolus amounts and adds basal amount
	  // for remaining 24 hrs to give you an estimate of the daily maximum infusion
	public float bolusDosageTotal;
		
	private Map<String,BasalProfile> profiles;
	private BasalProfile currentProfile;
	Timer bolusTimer;

	public InsulinManager()
	{
		bolusDosageTotal = 0;
		profiles = new HashMap<String,BasalProfile>();
		profiles.put("No Exercise", new BasalProfile("No Exercise", 5, 7, 6, 5));
		profiles.put("Light Exercise", new BasalProfile("Light Exercise", 5, 7, 6, 5));
		profiles.put("Heavy Exercise", new BasalProfile("Heavy Exercise", 4, 6, 5, 4));
		currentProfile = null;
		bolusTimer = new Timer(1000, bolusActionListener);
	}
	
	public String getCurrentProfileName() {
		if (currentProfile != null)
			return currentProfile.getName();
		
		return null;
	}
	
	public void setCurrentProfile(String name) {
		if (name != null)
			currentProfile = profiles.get(name);
		else
			currentProfile = null;
	}
	
	public Boolean isProfileLoaded() {
		if (currentProfile == null)
			return false;
		else
			return true;
	}
	
	/* Based on system time */
	public int getCurrentBasalRate() {
		int currHour = new Date().getHours();
		if (currHour >= 0 && currHour < 4)
			return currentProfile.getRate12amTo4am();
		else if (currHour >= 4 && currHour < 6)
			return currentProfile.getRate4amTo6am();
		else if (currHour >= 6 && currHour < 10)
			return currentProfile.getRate6amTo10am();
		else
			return currentProfile.getRate10amTo12am();
	}
	
	// Executes at every timeout
	ActionListener bolusActionListener = new ActionListener() {
		public void actionPerformed(ActionEvent evt) {
			
			bolusDosageTotal += 1;
			
			// Log
			Logger log = Logger.getLogger("log");
			log.info("Bolus dosage administered. (Total = " + bolusDosageTotal + ")");
		}
	};
	
	
	public void initiateBolus() {
		bolusTimer.start();
	}
}
